// 页面的.js文件
Page({
  data: {
    isSwiping: false, // 用于标记是否正在滑动
    items: [
      { name: 'Item 1', animation: null },
      { name: 'Item 2', animation: null },
      { name: 'Item 3', animation: null },
      // 更多的 items...
    ],
    currentSwiper: 0,
  },
  onLoad: function() {
    this.data.items.forEach(item=>{ 
      const animation = wx.createAnimation({
        duration: 0,
        timingFunction: 'linear',
      });
      item.animation = animation.scale(1).step();
      this.setData({
        items: this.data.items
      })
    })
  },
  touchStart: function(e) {
    if (this.data.isSwiping) {
      // 如果正在滑动，则阻止新的滑动开始
      return;
    }
    // 设置为正在滑动
    this.setData({ isSwiping: true });
    let that = this
    setTimeout(()=>{that.setData({ isSwiping: false });},4000)
  },
  onSwiperTransition: function(e) { 
    const dx = e.detail.dx;
    const { items, currentSwiper } = this.data;
    const scaleValue = 0.5; // 缩放的基础值

    items.forEach((item, index) => { 
      const animation = wx.createAnimation({
        duration: 0,
        timingFunction: 'linear',
      });

      let scale = scaleValue;
      if (index === currentSwiper) {
        scale = scaleValue + (1 - scaleValue) * (1 - Math.abs(dx / 375));
        // console.log(scale);
      } else if (index === currentSwiper + 1 && dx > 0) {
        scale = scaleValue + (1 - scaleValue) * (Math.abs(dx / 375) -1 );
        if (scale > 1) scale = 1
      } else if (index === currentSwiper - 1 && dx < 0) {
        scale = scaleValue + (1 - scaleValue) * (Math.abs(dx / 375) -1 );
        if (scale > 1) scale = 1
      }
      animation.scale(scale).step();
      item.animation = animation.export();

       
      // const animation2 = wx.createAnimation({
      //   duration: 0,
      //   timingFunction: 'linear',
      // });
      // animation2.scale(1.0/scale).step();
      // if (index+1 <= items.length -1 ){
      //   items[index+1].animation = animation.export();
      // }
      // if (index-1 >= 0){        
      //   items[index-1].animation = animation.export();
      // }


    });

    this.setData({ items });
  },
  onSwiperAnimationFinish: function(e) {
    // console.log(e);
    const { current } = e.detail;
    this.setData({ currentSwiper: current });
    // 设置为滑动结束
    this.setData({ isSwiping: false });
  },
});
